package com.ssy.lingxi.pay.controller.pc.assetAccount;

import com.ssy.lingxi.common.constant.order.OrderPayChannelEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderPayParameterFeignVO;
import com.ssy.lingxi.order.api.model.vo.response.PaymentParameterFeignDetailVO;
import com.ssy.lingxi.pay.api.dto.request.assetAccount.*;
import com.ssy.lingxi.pay.api.dto.response.assetAccount.AccountRechargeResponse;
import com.ssy.lingxi.pay.api.dto.response.assetAccount.MemberAssetAccountResponse;
import com.ssy.lingxi.pay.entity.assetAccount.AccountStatusRecord;
import com.ssy.lingxi.pay.entity.assetAccount.AccountTradeRecord;
import com.ssy.lingxi.pay.entity.assetAccount.MemberAssetAccount;
import com.ssy.lingxi.pay.model.assetAccount.BalanceQueryVO;
import com.ssy.lingxi.pay.model.assetAccount.MemberAssetAccountQuery;
import com.ssy.lingxi.pay.service.assetAccount.IAccountStatusService;
import com.ssy.lingxi.pay.service.assetAccount.IAccountTradeService;
import com.ssy.lingxi.pay.service.assetAccount.IMemberAssetAccountService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu: 资金账户管理
 * 资金账户
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/pay/assetAccount")
public class AssetAccountController extends BaseController {

    @Resource
    private IMemberAssetAccountService memberAssetAccountService;

    @Resource
    private IAccountTradeService accountTradeService;

    @Resource
    private IAccountStatusService accountStatusService;

    @Resource
    private OrderFeignService orderFeignService;

    /**
     * 查询资金账户列表
     * @param pageVO 分页实体
     * @param assetAccountQueryRequest
     */
    @RequestMapping(value = "/getAssetAccountList", method = RequestMethod.GET)
    public Wrapper<PageData<MemberAssetAccountResponse>> getAssetAccountList(PageVO pageVO, AssetAccountQueryRequest assetAccountQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        MemberAssetAccountQuery memberAssetAccountMiddle = this.modelMapper.map(assetAccountQueryRequest, MemberAssetAccountQuery.class);
        memberAssetAccountMiddle.setMemberId(memberId);
        memberAssetAccountMiddle.setMemberRoleId(memberRoleId);
        Page<MemberAssetAccount> result = memberAssetAccountService.getMemberAssetAccountList(pageVO, memberAssetAccountMiddle);
        List<MemberAssetAccountResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<MemberAssetAccountResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 账户详情
     * @param id 资金账户id
     */
    @RequestMapping(value = "/getAssetAccount", method = RequestMethod.GET)
    public Wrapper<MemberAssetAccountResponse> getAssetAccount(@RequestParam Long id) {
        MemberAssetAccount memberAssetAccount = memberAssetAccountService.getMemberAssetAccount(id);
        if(memberAssetAccount != null){
            return Wrapper.success(this.modelMapper.map(memberAssetAccount, MemberAssetAccountResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 交易记录
     * @param memberAssetAccountId 会员资金账户id
     */
    @RequestMapping(value = "/getAccountTradeRecord", method = RequestMethod.GET)
    public Wrapper<PageData<AccountTradeRecord>> getAccountTradeRecord(PageVO pageVO, @RequestParam("memberAssetAccountId") Long memberAssetAccountId) {
        Page<AccountTradeRecord> result = accountTradeService.getAccountTradeRecord(pageVO, memberAssetAccountId);
        return Wrapper.success(new PageData<>(result.getTotalElements(), result.getContent()));
    }

    /**
     * 状态记录
     * @param memberAssetAccountId 会员资金账户id
     */
    @RequestMapping(value = "/getAccountStatusRecord", method = RequestMethod.GET)
    public Wrapper<List<AccountStatusRecord>> getAccountStatusRecord(@RequestParam("memberAssetAccountId") Long memberAssetAccountId) {
        return Wrapper.success(accountStatusService.getAccountStatusRecord(memberAssetAccountId));
    }

    /**
     * 账户充值
     * @param rechargeRequest 参数
     */
    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    public Wrapper<AccountRechargeResponse> recharge(@RequestBody @Valid RechargeRequest rechargeRequest, HttpServletRequest request) {
        return Wrapper.success(memberAssetAccountService.rechargeAssetAccount(request, rechargeRequest));
    }

    /**
     * 查询账户充值结果
     * @param tradeRecordId 交易记录id
     */
    @RequestMapping(value = "/getRechargeResult", method = RequestMethod.GET)
    public Wrapper<Boolean> getRechargeResult(@RequestParam("tradeRecordId") Long tradeRecordId) {
        return Wrapper.success(memberAssetAccountService.getRechargeResult(tradeRecordId));
    }

    /**
     * 申请提现
     * @param cashOutRequest 参数
     */
    @RequestMapping(value = "/cashOut", method = RequestMethod.POST)
    public Wrapper<String> cashOut(@RequestBody @Valid CashOutRequest cashOutRequest) {
        return Wrapper.success(memberAssetAccountService.cashOutAssetAccount(cashOutRequest));
    }

    /**
     * 余额支付
     * @param balancePayRequest 参数
     */
    @RequestMapping(value = "/balancePay", method = RequestMethod.POST)
    public Wrapper<String> balancePay(@RequestBody BalancePayRequest balancePayRequest) {
        return Wrapper.success(memberAssetAccountService.balancePay(balancePayRequest));
    }

    /**
     * 余额返现
     * @param balanceCashbackRequest 参数
     */
    @RequestMapping(value = "/balanceCashback", method = RequestMethod.POST)
    public Wrapper<String> balanceCashback(@RequestBody BalanceCashbackRequest balanceCashbackRequest) {
        return Wrapper.success(memberAssetAccountService.balanceCashback(balanceCashbackRequest));
    }

    /**
     * 余额退款
     * @param balanceRefundRequest 参数
     */
    @RequestMapping(value = "/balanceRefund", method = RequestMethod.POST)
    public Wrapper<Boolean> balanceRefund(@RequestBody BalanceRefundRequest balanceRefundRequest) {
        return Wrapper.success(memberAssetAccountService.balanceRefund(balanceRefundRequest));
    }

    /**
     * 查询会员账户余额
     * @param queryVO 接口参数
     */
    @RequestMapping(value = "/balance", method = RequestMethod.GET)
    public Wrapper<Double> getAccountBalance(@Valid BalanceQueryVO queryVO) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        return Wrapper.success(memberAssetAccountService.getUserBalance(queryVO.getFundMode(), memberId, memberRoleId, queryVO.getVendorMemberId(), queryVO.getVendorRoleId()));
    }

    /**
     * 查询登录用户的账户余额
     * @param parentMemberId 上级会员id
     * @param parentMemberRoleId 上级会员角色id
     */
    @RequestMapping(value = "/getUserBalance", method = RequestMethod.GET)
    public Wrapper<Double> getUserBalance(@RequestParam Long parentMemberId, @RequestParam Long parentMemberRoleId) {
        //从会员服务查询资金归集模式
        OrderPayParameterFeignVO feignVO = new OrderPayParameterFeignVO();
        feignVO.setMemberId(parentMemberId);
        feignVO.setRoleId(parentMemberRoleId);
        feignVO.setPayChannel(OrderPayChannelEnum.ACCOUNT_BALANCE);
        Wrapper<PaymentParameterFeignDetailVO> parameterResult = orderFeignService.findPaymentParameters(feignVO);
        if(parameterResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(parameterResult.getCode(), parameterResult.getMessage());
        }

        UserLoginCacheDTO sysUser = this.getSysUser();
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        return Wrapper.success(memberAssetAccountService.getUserBalance(parameterResult.getData().getFundMode().getCode(), memberId, memberRoleId, parentMemberId, parentMemberRoleId));
    }

//    /**
//     * 查询登录用户的账户余额
//     * @param parentMemberId 上级会员id
//     * @param parentMemberRoleId 上级会员角色id
//     */
//    @RequestMapping(value = "/getUserBalance", method = RequestMethod.GET)
//    public Wrapper<Double> getUserBalance(@RequestParam Long parentMemberId, @RequestParam Long parentMemberRoleId) {
//        //查询当前用户的资金归集模式: 1-平台，2-会员
//        Wrapper<Long> wrapper = paymentPolicyService.fundCollectionMode(parentMemberId,parentMemberRoleId, PayWayTypeEnum.YUE.getId());
//        if(ResponseCode.SUCCESS.getCode() == wrapper.getCode()){
//            Long payWay = wrapper.getData();
//            if(payWay != null && payWay > 0){
//                UserLoginCacheDTO sysUser = this.getSysUser();
//                Long memberId = sysUser.getMemberId();
//                Long memberRoleId = sysUser.getMemberRoleId();
//                return Wrapper.success(memberAssetAccountService.getUserBalance(payWay, memberId, memberRoleId, parentMemberId, parentMemberRoleId));
//            }else{
//                return Wrapper.fail("查询资金归集模式失败");
//            }
//        }else{
//            return Wrapper.fail(wrapper.getMessage());
//        }
//    }

    /**
     * 查询采购商的账户余额
     * @param childMemberId 下级会员id
     * @param childMemberRoleId 下级会员角色id
     */
    @RequestMapping(value = "/getChildUserBalance", method = RequestMethod.GET)
    public Wrapper<Double> getChildUserBalance(@RequestParam Long childMemberId, @RequestParam Long childMemberRoleId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(memberAssetAccountService.getChildUserBalance(sysUser.getMemberId(), sysUser.getMemberRoleId(), childMemberId, childMemberRoleId));
    }

    /**
     * 查询登录用户的所有账户余额
     */
    @RequestMapping(value = "/getUserAllBalance", method = RequestMethod.GET)
    public Wrapper<Double> getUserAllBalance() {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        return Wrapper.success(memberAssetAccountService.getUserAllBalance(memberId, memberRoleId));
    }
}
